package com.vhuomai.product.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Scope;
import com.vhuomai.product.entity.BaseProductCategory;
import com.vhuomai.product.service.ProductCategoryService;
import com.vhuomai.system.constant.UserRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author harris
 */
@Api(tags = "商品目录")
@RestController
@RequestMapping("/api")
@Scope(role = UserRole.MANAGER)
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService categoryService;

    @ApiOperation(value = "商品目录")
    @GetMapping(value = "/product/categories", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<List<BaseProductCategory>> categories(@RequestParam("parentId") Integer parentId) {
        return ServiceResult.success(categoryService.findByParentId(parentId));
    }

    @ApiOperation(value = "添加目录")
    @PostMapping(value = "/product/categories", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<BaseProductCategory> addCategory(@RequestBody BaseProductCategory category) {
        return ServiceResult.success(categoryService.add(category));
    }

    @ApiOperation(value = "修改目录")
    @PutMapping(value = "/product/categories", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> updateCategory(@RequestBody BaseProductCategory category) {
        categoryService.update(category);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除目录")
    @DeleteMapping(value = "/product/categories", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> removeCategory(@RequestParam("id") Integer id) {
        categoryService.remove(id);
        return ServiceResult.SUCCESS;
    }
}
